import 'dart:convert';

import 'package:atcflutter/base/network/atc_http_util.dart';
import 'package:atcflutter/user/bean/user_entity.dart';
import 'package:zcore/utils/security_util.dart';
import 'package:zcore/utils/sharedpreference_util.dart';

class UserModel {
  static const String KEY_USER_INFO = "key_user_info";
  static const String URL_LOGIN_IN =
      "http://dealercloudapi.che168.com/tradercloud/v120/login/login.ashx";


  ///登录操作
  static login(String account, String pwd) {
    return ATCHttpUtil.post(URL_LOGIN_IN,
        {"username": account, "pwd": SecurityUtil.generateMd5(pwd)});
  }

  /// 是否登录
  static Future<bool> isLogin() async {
    return await getUserInfo() != null;
  }

  /// 存储用户信息
  static Future<dynamic> setUserInfo(Map<String, dynamic> userEntity) {
    return SharedPreferenceUtil.getInstance()
        .saveString(KEY_USER_INFO, jsonEncode(userEntity));
  }

  /// 获取用户信息
  static Future<UserEntity> getUserInfo() {
    return SharedPreferenceUtil.getInstance()
        .getString(KEY_USER_INFO)
        .then((source) {
      return source == null ? null : UserEntity().fromJson(jsonDecode(source));
    });
  }
}
